﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace Majoopic.WPFClient.Commands
{
    /// <summary>
    /// Genereic command class to specify the action for execute and canexecute
    /// </summary>
    public class GenericCommand : ICommand
    {
        private Action<object> _executeCommand;

        private Func<object, bool> _canExecuteCommand;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="executeCommand">Execute delegate</param>
        /// <param name="canExecuteCommand">CanExecute delegate</param>
        public GenericCommand(Action<object> executeCommand, Func<object, bool> canExecuteCommand = null)
        {
            this._executeCommand = executeCommand;
            this._canExecuteCommand = canExecuteCommand;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public bool CanExecute(object parameter)
        {
            if (this._canExecuteCommand != null)
            {
                return this._canExecuteCommand(parameter);
            }
            return true;
        }

        /// <summary>
        /// Event raised when the canexecuted changed
        /// </summary>
        public event EventHandler CanExecuteChanged;

        /// <summary>
        /// Raise the CanExecuteChanged event
        /// </summary>
        public void RaiseCanExecuteChanged()
        {
            if (this.CanExecuteChanged != null)
            {
                this.CanExecuteChanged(this, EventArgs.Empty);
            }
        }

        /// <summary>
        /// Execute the command
        /// </summary>
        /// <param name="parameter"></param>
        public void Execute(object parameter)
        {
            this._executeCommand(parameter);
        }
    }
}
